<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Member extends Model
{
    protected $table = 'ims_mc_members';
    
    protected $primaryKey = 'uid';
    
    public $incrementing = true;
    
    public $timestamps = false;
    
    protected $fillable = [
        'uid',
        'uniacid',
        'mobile',
        'email',
        'password',
        'salt',
        'groupid',
        'credit1',
        'credit2',
        'credit3',
        'credit4',
        'credit5',
        'credit6',
        'createtime',
        'realname',
        'nickname',
        'avatar',
        'qq',
        'vip',
        'gender',
        'birthyear',
        'birthmonth',
        'birthday',
        'constellation',
        'zodiac',
        'telephone',
        'idcard',
        'studentid',
        'grade',
        'address',
        'zipcode',
        'nationality',
        'resideprovince',
        'residecity',
        'residedist',
        'graduateschool',
        'company',
        'education',
        'occupation',
        'position',
        'revenue',
        'affectivestatus',
        'lookingfor',
        'bloodtype',
        'height',
        'weight',
        'alipay',
        'msn',
        'taobao',
        'site',
        'bio',
        'interest',
        'pay_password',
        'agentid',
        'agent_level'
    ];
    
    protected $casts = [
        'uid' => 'integer',
        'uniacid' => 'integer',
        'groupid' => 'integer',
        'credit1' => 'decimal:2',
        'credit2' => 'decimal:2',
        'credit3' => 'decimal:2',
        'credit4' => 'decimal:2',
        'credit5' => 'decimal:2',
        'credit6' => 'decimal:2',
        'createtime' => 'integer',
        'vip' => 'integer',
        'gender' => 'integer',
        'birthyear' => 'integer',
        'birthmonth' => 'integer',
        'birthday' => 'integer'
    ];
    
    /**
     * 获取真实姓名
     */
    public function getRealNameAttribute()
    {
        return $this->attributes['realname'] ?: $this->attributes['nickname'] ?: '未知';
    }
    
    /**
     * 获取显示名称
     */
    public function getDisplayNameAttribute()
    {
        return $this->attributes['realname'] ?: $this->attributes['nickname'] ?: '用户' . $this->uid;
    }
    
    /**
     * 获取完整地址
     */
    public function getFullAddressAttribute()
    {
        $address = '';
        if ($this->resideprovince) {
            $address .= $this->resideprovince;
        }
        if ($this->residecity) {
            $address .= $this->residecity;
        }
        if ($this->residedist) {
            $address .= $this->residedist;
        }
        if ($this->address) {
            $address .= $this->address;
        }
        return $address ?: '未设置地址';
    }
    
    /**
     * 关联供应商（作为申请人）
     */
    public function vendorApplications()
    {
        return $this->hasMany(Vendor::class, 'applicant_id', 'uid');
    }
    
    /**
     * 关联供应商（作为推荐人）
     */
    public function vendorReferrals()
    {
        return $this->hasMany(Vendor::class, 'referrer_id', 'uid');
    }
    
    /**
     * 关联订单（用于审核系统）
     */
    public function orders()
    {
        return $this->hasMany(Order::class, 'uid', 'uid');
    }
    
    /**
     * 获取头像URL（用于审核系统）
     */
    public function getAvatarUrlAttribute()
    {
        return $this->avatar ?: '/static/images/default-avatar.png';
    }
}